/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dominio;

import java.util.regex.*;
import java.util.StringTokenizer;

/**
 *
 * @author Juan Manuel Fuentes Diaz
 */
public class ParserTXT {

    private StringBuffer titulo;
    private StringBuffer texto;

    public TitTex sacaTexto(StringBuffer str) throws NoticiaNoValidaException {
        TitTex tt = new TitTex();
        //try {
        this.crearParserDisc();
        str = new StringBuffer(tagMayMin(str.toString()));
        contieneTitulo(str);
        contieneTextoP(str);
        //} catch (Exception e) {
        //    throw new NoticiaNoValidaException("Noticia no v\u00e1lida: " + e.getMessage());
        //}
        if (!esNoticia()) {
            throw new NoticiaNoValidaException("Noticia no v\u00e1lida");
        } else {
            tt.titulo = this.titulo.toString();
            tt.texto = this.texto.toString();
             }
        return tt;

    }

    private boolean esNoticia() {
        return (this.titulo.length() != 0 && this.texto.length() != 0);
    }

    private void crearParserDisc() {
        this.titulo = new StringBuffer("");
        this.texto = new StringBuffer("");
    }

    private void contieneTitulo(StringBuffer str) {
        if ((str.indexOf("-titulo-") != -1) && (str.indexOf("-destino-") != -1)) {
            String s1 = str.substring(str.indexOf("-titulo-") + 8, str.indexOf("-destino-"));
            s1 = tokenizarCarext(s1);
            //s1 = new String(tokenizarSig(s1));
            s1 = new String(quitaEspacioFinal(s1));
            this.titulo.append(s1);
        }
    }

    private void contieneTextoP(StringBuffer str) {
        if ((str.indexOf("-texto-") != -1) && (str.indexOf("-end-") != -1)) {
            String s1 = str.substring(str.indexOf("-texto-") + 7, str.indexOf("-end-"));
            s1 = tokenizarCarext(s1);
            s1 = new String(tokenizarSig(s1));
            s1 = new String(quitaEspacioFinal(s1));
            this.texto.append(s1);
        }

    }
    
    private String quitaEspacioFinal(String st) {
        if (st.charAt(st.length()-1)==' '){
            st = new String(st.substring(0, st.length()-1));
        }
        if (st.charAt(0)==' '){
            st = new String(st.substring(1, st.length()));
        }
        return st;
    }

    private String tokenizarCarext(String st) {
        String[] carext = {"'", "&pound;", "&rsaquo;", "&quot;", "&ldquo;", "&rdquo;", "&acute;", "&aacute;", "&eacute;", "&iacute;", "&oacute;", "&uacute;", "&Aacute;", "&Eacute;", "&Iacute;", "&Oacute;", "&Uacute;", "&agrave;", "&egrave;", "&igrave;", "&ograve;", "&ugrave;", "&Agrave;", "&Egrave;", "&Igrave;", "&Ograve;", "&Ugrave;", "&uml;", "&auml;", "&euml;", "&iuml;", "&ouml;", "&uuml;", "&Auml;", "&Euml;", "&Iuml;", "&Ouml;", "&Uuml;", "&amp;", "&lt;", "&gt;", "&iexcl;", "&iquest;", "&nbsp;", "&ccedil;", "&Ccedil;", "&ntilde;", "&Ntilde;"};
        String[] carlat = {"\u0027", "", "", "", "", "", "\u0060", "\u00E1", "\u00E9", "\u00ED", "\u00F3", "\u00FA", "\u00C1", "\u00C9", "\u00CD", "\u00D3", "\u00DA", "\u00E0", "\u00E8", "\u00EC", "\u00F2", "\u00F9", "\u00C0", "\u00C8", "\u00CC", "\u00D2", "\u00D9", "\u00A8", "\u00E4", "\u00EB", "\u00EF", "\u00F6", "\u00FC", "\u00C4", "\u00CB", "\u00CF", "\u00D6", "\u00DC", "\u0026", "\u003C", "\u003E", "\u00A1", "\u00BF", "\u00A0", "\u00E7", "\u00C7", "\u00F1", "\u00D1"};
        for (int i = 0; i < carext.length; i++) {
            st = new String(st.replaceAll(carext[i], carlat[i]));
        }
        return st;
    }

    private String tagMayMin(String st) {
        String[] tagmin = {"-titulo-", "-destino-", "-texto-", "-end-"};
        String[] tagmay = {"-TITULO-", "-DESTINO-", "-TEXTO-", "-END-"};
        for (int i = 0; i < tagmay.length; i++) {
            st = new String(st.replaceAll(tagmay[i], tagmin[i]));
        }
        return st;
    }

    private String tokenizarSig(String st) {
        Pattern pn = Pattern.compile("[a-zA-Z]");
        String pal;
        StringTokenizer stok = new StringTokenizer(st);
        int q = stok.countTokens();
        StringBuffer list = new StringBuffer("");
        char[] nacc = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
        char[] acc = {'\u00c0', '\u00c1', '\u00c2', '\u00c4', '\u00c8', '\u00c9', '\u00ca', '\u00cb', '\u00cc', '\u00cd', '\u00ce', '\u00cf', '\u00d2', '\u00d3', '\u00d4', '\u00d6', '\u00d9', '\u00da', '\u00db', '\u00dc', '\u00e0', '\u00e1', '\u00e2', '\u00e4', '\u00e8', '\u00e9', '\u00ea', '\u00eb', '\u00ec', '\u00ed', '\u00ee', '\u00ef', '\u00f2', '\u00f3', '\u00f4', '\u00f6', '\u00f9', '\u00fa', '\u00fb', '\u00fc'};
        for (int i = 0; i < q; i++) {
            pal = stok.nextToken();
            if ((pn.matcher(pal).find())) {
                for (int j = 0; j < acc.length; j++) {
                    pal = pal.replace(acc[j], nacc[j / 4]);
                }
                list.append(pal);
                list.append(" ");
            }
        }
        return list.toString();
    }
}

